package com.andy.qhb.entity;

import java.util.List;
import java.util.Map;

import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;

import com.andy.qhb.dao.DaoUtils;
import com.andy.qhb.vo.HFriend;
import com.google.common.collect.Lists;

public class UsrFriends extends BaseEntity{

	private static final long serialVersionUID = 1L;

	public UsrFriends() {
	}
	
	public UsrFriends(Long id) {
		super(id);
	}
	
	private Long uid;
	private String fids="";

	public Long getUid() {
		return uid;
	}

	public void setUid(Long uid) {
		this.uid = uid;
	}
	
	public static String table() {
		return "usr_friends";
	}

	public String getFids() {
		return fids;
	}

	public void setFids(String fids) {
		this.fids = fids;
	}

	private List<HFriend> friends;
	public List<HFriend> friends(){
		if(friends == null){
			friends = Lists.newArrayList();
			if(fids != null && fids.length() > 0){
				List<Map<String, Object>> list = DaoUtils.getListSql("select id,nick_name,icon from usr_user where id in ("+fids.substring(0, fids.length()-1)+")");
				list.forEach((it)->{
					friends.add(new HFriend(MapUtils.getLong(it, "id"),MapUtils.getString(it, "nick_name"),MapUtils.getString(it, "icon")));
				});
			}
		}
		
		return friends;
	}
	
	public void addFriend(Long uid,String name,String icon){
		if(fids.indexOf(uid+",") > 0)return;
		this.fids = fids + uid+",";
		friends().add(new HFriend(uid, name,icon));
	}

	public void remove(Long uid) {
		fids = StringUtils.remove(fids, uid+",");
		friends().remove(new HFriend(uid, null,null));
	}
	
}
